package top.juntower.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juntower.pojo.*;
import top.juntower.pojo.vo.GameTypeInformationVo;
import top.juntower.service.impl.GameTypeServiceImpl;
import top.juntower.utils.JsonResult;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "游戏类别")
@RestController
@RequestMapping("gametype")
public class GameTypeController {
    @Autowired
    GameTypeServiceImpl gameTypeService;


    //查询游戏类别
    @ApiOperation(value = "游戏类别",response = GameType.class)
    @GetMapping("/queryGameTypes")
    public JsonResult queryGameTypes(){
        List<GameType> gameTypes = gameTypeService.queryGameTypes();
        if (gameTypes == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(gameTypes);
    }

    //游戏类别查详细游戏
    @ApiOperation(value = "游戏类别查详细游戏",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtId", value = "游戏类别Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/GameTypeById")
    public JsonResult GameTypeById(Integer gtId,Integer pageNum){
        if (gtId == null || pageNum == null){
           return JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,16);
        List<GameTypeInformation> gameTypeInformations = gameTypeService.GameTypeById(gtId,pageNum,16);
        PageInfo<GameTypeInformation> pageInfo = new PageInfo<GameTypeInformation>(gameTypeInformations);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //游戏信息
    @ApiOperation(value = "游戏信息",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id（用户Id可以不写）", dataType = "Integer", required = true)
    })
    @GetMapping("/gameInformation")
    public JsonResult gameInformation(Integer giId,Integer uId){
        if (giId == null){
          return   JsonResult.error(JsonResult.NO);
        }
        if (uId == null){
            GameInformation gameInformation = gameTypeService.gameInformation(giId);
            gameInformation.setCgState(false);
            return JsonResult.ok(gameInformation);
        }
        String collectionGame = gameTypeService.isCollectionGame(giId, uId);
        GameInformation gameInformation = gameTypeService.gameInformation(giId);
        if (collectionGame == null){
            if (gameInformation == null){
                return JsonResult.error(JsonResult.MISS);
            }
            gameInformation.setCgState(false);
            return  JsonResult.ok(gameInformation);
        }else {

            if (gameInformation == null){
                return JsonResult.error(JsonResult.MISS);
            }
            gameInformation.setCgState(true);
            return  JsonResult.ok(gameInformation);
        }
    }

    //游戏类别排行游戏信息
    @ApiOperation(value = "游戏类别排行游戏信息",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gtId", value = "游戏类别Id", dataType = "Integer", required = true)
    })
    @GetMapping("/gameTypeInformationPaiHang")
    public JsonResult gameTypeInformationPaiHang(Integer gtId){
        if (gtId == null){
           return JsonResult.error(JsonResult.NO);
        }
        List<GameTypeInformationVo> gameTypeInformationVos = gameTypeService.gameTypeInformationPaiHang(gtId);
        if (gameTypeInformationVos == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(gameTypeInformationVos);
    }

    //添加收藏
    @ApiOperation(value = "添加收藏",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cgState", value = "收藏状态", dataType = "String", required = true),
    })
    @GetMapping("/collectionGame")
    public JsonResult collectionGame(Integer giId,Integer uId,String cgState){
        if (giId == null || uId == null || cgState == null){
          return  JsonResult.error(JsonResult.NO);
        }
        String isCollectionGame = gameTypeService.CollectionGame(giId, uId);
        Integer state;
        if (cgState.equals("false")){
            state = 1;
        }else {
            state = 0;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("uId",uId);
        map.put("giId",giId);
        map.put("cgState",state);
        map.put("cgTime",new Date());
        if (isCollectionGame == null){
            int i = gameTypeService.addCollectionGame(map);
            if (i>0){
                return  JsonResult.ok("添加成功");
            }else {
                return  JsonResult.error(JsonResult.UPDATE_MISS);
            }

        }else {
            int i = gameTypeService.updateCollectionGame(map);
            if (i>0){
                return  JsonResult.ok("修改成功");
            }else {
                return  JsonResult.error(JsonResult.UPDATE_MISS);
            }
        }
    }

    //根据访问量排行
    @ApiOperation(value = "根据访问量排行",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/trafficRanking")
    public JsonResult trafficRanking(Integer pageNum){
        if (pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,20);
        List<GameTrafficRanking> gameTrafficRankings = gameTypeService.trafficRanking(pageNum,20);
        PageInfo<GameTrafficRanking> pageInfo = new PageInfo<GameTrafficRanking>(gameTrafficRankings);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //根据评分排行
    @ApiOperation(value = "根据评分排行",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/scoreRanking")
    public JsonResult scoreRanking(Integer pageNum){
        if (pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,20);
        List<GameTrafficRanking> gameScoreRanking = gameTypeService.scoreRanking(pageNum, 20);
        PageInfo<GameTrafficRanking> pageInfo = new PageInfo<GameTrafficRanking>(gameScoreRanking);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //根据新游热度排行
    @ApiOperation(value = "根据新游热度排行",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/newHotRanking")
    public JsonResult newHotRanking(Integer pageNum){
        if (pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,20);
        List<GameTrafficRanking> gameTrafficRankings = gameTypeService.newHotRanking(pageNum, 20);
        PageInfo<GameTrafficRanking> pageInfo = new PageInfo<GameTrafficRanking>(gameTrafficRankings);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }


    //根据国内热度排行
    @ApiOperation(value = "根据国内热度排行",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/domesticHotRanking")
    public JsonResult domesticHotRanking(Integer pageNum){
        if (pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,20);
        List<GameTrafficRanking> gameTrafficRankings = gameTypeService.domesticHotRanking(pageNum, 20);
        PageInfo<GameTrafficRanking> pageInfo = new PageInfo<GameTrafficRanking>(gameTrafficRankings);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //根据国外热度排行
    @ApiOperation(value = "根据国外热度排行",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/abroadHotRanking")
    public JsonResult abroadHotRanking(Integer pageNum){
        if (pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,20);
        List<GameTrafficRanking> gameTrafficRankings = gameTypeService.abroadHotRanking(pageNum, 20);
        PageInfo<GameTrafficRanking> pageInfo = new PageInfo<GameTrafficRanking>(gameTrafficRankings);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }

    //增加访问量
    @ApiOperation(value = "增加访问量",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
    })
    @GetMapping("/addVisits")
    public JsonResult addVisits(Integer giId){
        if (giId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = gameTypeService.addVisits(giId);
        if(i>0){
            return JsonResult.ok("增加成功");
        }
        return  JsonResult.error(JsonResult.UPDATE_MISS);
    }

    //游戏模糊查询
    @ApiOperation(value = "游戏模糊查询",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "likeName", value = "模糊名", dataType = "string", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/likeGame")
    public JsonResult likeGame(String likeName,Integer pageNum){
        if (likeName == null || pageNum== null){
            return JsonResult.error(JsonResult.MISS);
        }
        PageHelper.startPage(pageNum,12);
        List<GameTypeInformation> gameTypeInformations = gameTypeService.likeGame(likeName, pageNum, 12);
        PageInfo<GameTypeInformation> pageInfo = new PageInfo<GameTypeInformation>(gameTypeInformations);
        if (pageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(pageInfo);
    }
    
}
